extends CanvasLayer

func fade_switch(texture: Texture2D):
	$LightLevel.show()
	$FadeSwitch.play("fade_switch")
	await $FadeSwitch.animation_finished
	$BackgroundImage.texture = texture
	$FadeSwitch.play_backwards("fade_switch")
	await $FadeSwitch.animation_finished
	$LightLevel.hide()

	# Reset the timer
	$"/root/Game/Mechanics/BackgroundTimer".start()

func _on_background_timer_timeout() -> void:
	if Global.G_phase == Global.Phase.Day:
		var idx_day : int = Preload.Background_Day.find($BackgroundImage.texture)
		fade_switch(Preload.Background_Day[(idx_day + 1) % Preload.Background_Day.size()])
		return

	var idx_night : int = Preload.Background_Night.find($BackgroundImage.texture)
	fade_switch(Preload.Background_Night[(idx_night + 1) % Preload.Background_Night.size()])
